package com.itheima.web.controller;


import com.itheima.vo.MovementVo;
import com.itheima.vo.PageBeanVo;
import com.itheima.vo.UserInfoVo;
import com.itheima.web.manager.UserManager;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
public class UserController {

    @Autowired
    private UserManager userManager;

    //用户列表
    @GetMapping("/manage/users")
    public PageBeanVo findUserList(
            @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize){
        return userManager.findUserList(pageNum,pageSize);
    }
    //用户详情信息
    @GetMapping("/manage/users/{userID}")
    public UserInfoVo findUserInfo(@PathVariable("userID") Long userId) {
        return userManager.findUserInfo(userId);
    }

    //查询指定用户的视频列表
    @GetMapping("/manage/videos")
    public PageBeanVo findUserVideoList(Long uid,
                                        @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
                                        @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize){
         return userManager.findUserVideoList(uid, pageNum, pageSize);
    }

    //用户动态
    @GetMapping("/manage/messages")
    public PageBeanVo findUserMovementList(Long uid, String state,
                                           @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
                                           @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
        //处理state
        Integer stateInt = null;
        if (StringUtils.isNotEmpty(state) && !StringUtils.equals("''", state)) {
            stateInt = Integer.parseInt(state);
        }

        return userManager.findUserMovementList(uid, stateInt, pageNum, pageSize);
    }
    //用户动态详情
    @GetMapping("/manage/messages/{id}")
    public MovementVo findMovementDetail(@PathVariable("id") String movementId) {
        return userManager.findMovementDetail(movementId);
    }

    //用户动态评论
    @GetMapping("/manage/messages/comments")
    public PageBeanVo findMovementCommentList(@RequestParam("messageID") String movementId,
                                              @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
                                              @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
        return userManager.findMovementCommentList(movementId, pageNum, pageSize);
    }


    //todo 动态审核——通过待审核、驳回动态
    @PostMapping("/manage/messages/pass")
    public Map<String,String> passMovement(@RequestBody String[] movementIdArr){
        System.out.println("动态通过接到前端数据为："+movementIdArr);
       return userManager.passMovement(movementIdArr);
    }
    //todo 动态审核——驳回待审核动态
    @PostMapping("/manage/messages/reject")
    public Map<String,String> rejectMovement(@RequestBody String[] movementIdArr){
        System.out.println("动态驳回接到前端数据为："+movementIdArr);
        return userManager.rejectMovement(movementIdArr);
    }
    //冻结用户
    @PostMapping("/manage/users/freeze")
    public Map<String, String> freezeUser(@RequestBody Map<String,String> map)
    {
        System.out.println(map);
        Integer userId =Integer.parseInt(map.get("userId")) ;
        Integer freezingTime =Integer.parseInt(map.get("freezingTime")) ;
        Integer freezingRange = Integer.parseInt(map.get("freezingRange")) ;
        String reasonsForFreezing = (String) map.get("reasonsForFreezing");
        String frozenRemarks = (String) map.get("frozenRemarks");

       String result=userManager.freezeUser(userId,freezingRange,freezingTime,reasonsForFreezing,frozenRemarks);
        Map<String,String> resultMap= new HashMap<String,String>();
        map.put("message",result);
        return resultMap;
    }
    //解冻
@PostMapping("/manage/users/unfreeze")
    public void unFreeze(@RequestBody Map<String,String> map){
    Integer id = Integer.valueOf(map.get("userId"));
    String reasonsForThawing = map.get("reasonsForThawing");
    userManager.unFreeze(id,reasonsForThawing);

}
}
